<template>
  <el-dialog
    :title="!fieldDataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      size="small"
      :model="fieldDataForm"
      :rules="dataRule"
      ref="fieldDataForm"
      @keyup.enter.native="fieldDataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="字段名称" prop="name">
        <el-input
          v-model="fieldDataForm.name"
          placeholder="字段名称"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="字段物理名称"
        v-if="physicalNameShow"
        prop="physicalName"
      >
        <el-input
          :disabled="physicalNameDisabled"
          v-model="fieldDataForm.physicalName"
          placeholder="字段物理名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="字段编码" prop="code">
        <el-input
          v-model="fieldDataForm.code"
          placeholder="字段编码"
        ></el-input>
      </el-form-item>

      <el-form-item label="字段属性" prop="fieldProperty">
        <el-radio-group v-model="fieldDataForm.fieldProperty">
          <el-radio :label="1">逻辑字段</el-radio>
          <el-radio :label="2">物理字段</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="字段类型" prop="fieldType">
        <el-popover
          ref="propertyFieldTypeListPopover"
          placement="bottom-start"
          trigger="click"
        >
          <el-tree
            :data="prpropertyFieldTypeList"
            :props="prpropertyListTreeProps"
            node-key="id"
            ref="prpropertyFieldTypeListTree"
            @current-change="prpropertyFieldTypeTreeCurrentChangeHandle"
            :default-expand-all="false"
            :highlight-current="true"
            :expand-on-click-node="false"
          >
          </el-tree>
        </el-popover>
        <el-input
          v-model="fieldDataForm.fieldTypeName"
          v-popover:propertyFieldTypeListPopover
          :readonly="true"
          placeholder="点击选择字段类型"
          class="menu-list__input"
        ></el-input>
      </el-form-item>

      <el-form-item label="枚举类型" prop="enumId" v-if="enumShow">
        <el-popover
          ref="propertyFieldEnumListPopover"
          placement="bottom-start"
          trigger="click"
        >
          <el-tree
            :data="prpropertyFieldEnumList"
            :props="prpropertyListTreeProps"
            node-key="id"
            ref="prpropertyFieldEnumListTree"
            @current-change="prpropertyFieldEnumTreeCurrentChangeHandle"
            :default-expand-all="false"
            :highlight-current="true"
            :expand-on-click-node="false"
          >
          </el-tree>
        </el-popover>
        <el-input
          v-model="fieldDataForm.enumeName"
          v-popover:propertyFieldEnumListPopover
          :readonly="true"
          placeholder="点击选择枚举类型"
          class="menu-list__input"
        ></el-input>
      </el-form-item>

      <el-form-item label="字段长度" v-if="fieldLengthShow">
        <el-input-number
          v-model="fieldDataForm.fieldLength"
          controls-position="right"
          :min="0"
          label="字段长度"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="是否必填">
        <el-radio-group v-model="fieldDataForm.required">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="建立索引">
        <el-radio-group v-model="fieldDataForm.index">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="提示信息">
        <el-input
          v-model="fieldDataForm.tooltip"
          placeholder="提示信息"
        ></el-input>
      </el-form-item>

      <el-form-item label="字段备注">
        <el-input v-model="fieldDataForm.remark" placeholder="备注"></el-input>
      </el-form-item>

      <el-form-item label="排序号">
        <el-input-number
          v-model="fieldDataForm.orderNum"
          controls-position="right"
          :min="0"
          label="排序号"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="表状态" size="mini">
        <el-radio-group v-model="fieldDataForm.dataState">
          <el-radio :label="0">正常</el-radio>
          <el-radio :label="-1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="默认值" size="mini">
        <el-input
          v-model="fieldDataForm.defaultValue"
          placeholder="默认值"
        ></el-input>
        <el-tag type="warning"
          >规则：默认值的有效字符长度受字段长度的控制</el-tag
        >
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="fieldDataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>


<script>
import { pageFieldProperty, saveOrUpdateField } from "@/api/table";
import { pageList as enumPageList } from "@/api/dict";
import { treeDataTranslate } from "@/utils";
export default {
  data() {
    var validateDefaultValue = (rule, value, callback) => {
      if (this.fieldDataForm.fieldLength > 0 && !/\S/.test(value)) {
        callback(new Error("菜单URL不能为空"));
      } else {
        callback();
      }
    };
    var validateEnumId = (rule, value, callback) => {
      if (this.fieldDataForm.fieldLength > 0 && !/\S/.test(value)) {
        callback(new Error("菜单URL不能为空"));
      } else {
        callback();
      }
    };

    return {
      visible: false,
      physicalNameShow: false,
      physicalNameDisabled: false,
      // TODO 这里需要重新设置下
      fieldLengthShow: true,
      enumShow: false,
      fieldDataForm: {
        id: "",
        code: "",
        parentId: "",
        physicalName: "",
        tooltip: "",
        fieldType: "",
        name: "",
        index: 0,
        required: 0,
        fieldLength: "",
        fieldProperty: "",
        orderNum: 0,
        defaultValue: "",
        dataState: 0,
        remark: "",
        fieldTypeName: "",
        enumId: "",
        enumeName: "",
      },
      dataRule: {
        name: [
          { required: true, message: "字段名称不能为空", trigger: "blur" },
        ],
        code: [
          { required: true, message: "字段编码不能为空", trigger: "blur" },
        ],
        fieldProperty: [
          { required: true, message: "字段属性不能为空", trigger: "blur" },
        ],
        fieldType: [
          { required: true, message: "字段类型不能为空", trigger: "blur" },
        ],
        defaultValue: [{ validator: validateDefaultValue, trigger: "blur" }],
        enumId: [{ validator: validateEnumId, trigger: "blur" }],
      },
      prpropertyFieldEnumList: [],
      prpropertyFieldTypeList: [],
      prpropertyListTreeProps: {
        label: "name",
        children: "children",
      },
    };
  },
  components: {},
  activated() {},
  methods: {
    init(data) {
      this.fieldDataForm.id = 0;

      enumPageList({})
        .then((res) => {
          this.dataListLoading = true;
          if (res && res.code === 0) {
            this.prpropertyFieldEnumList = treeDataTranslate(res.data, "id");
          }
          this.dataListLoading = false;
        })
        .then(() => {
          if (data.addChildren == 2) {
            this.fieldDataForm.enumId = data.enumId;
            this.prpropertyFieldEnumTreeSetCurrentNode();
          }
        });

      pageFieldProperty({})
        .then((res) => {
          this.dataListLoading = true;
          if (res && res.code === 0) {
            this.prpropertyFieldTypeList = treeDataTranslate(res.data, "id");
          }
          this.dataListLoading = false;
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["fieldDataForm"].resetFields();
          });
        })
        .then(() => {
          // 添加子节点
          if (data.addChildren == 1) {
            this.fieldDataForm.parentId = data.id;
            this.physicalNameShow = false;
            this.physicalNameDisabled = false;
            const fieldType =
              this.prpropertyFieldTypeList.length > 0
                ? this.prpropertyFieldTypeList[0].id
                : 0;
            this.fieldDataForm.fieldType = fieldType;
            this.prpropertyFieldTypeTreeSetCurrentNode();
          } else if (data.addChildren == 2) {
            // 编辑
            this.physicalNameShow = true;
            this.physicalNameDisabled = true;
            // 修改
            this.fieldDataForm.id = data.id;
            this.fieldDataForm.name = data.name;
            this.fieldDataForm.parentId = data.parentId;
            this.fieldDataForm.physicalName = data.physicalName;

            this.fieldDataForm.tooltip = data.tooltip;
            this.fieldDataForm.enumId = data.enumId;
            this.fieldDataForm.index = data.index;
            this.fieldDataForm.required = data.required;
            this.fieldDataForm.fieldLength = data.fieldLength;
            this.fieldDataForm.fieldProperty = data.fieldProperty;
            this.fieldDataForm.fieldType = data.fieldType;

            this.fieldDataForm.orderNum = data.orderNum;
            this.fieldDataForm.defaultValue = data.defaultValue;
            this.fieldDataForm.remark = data.remark;
            this.fieldDataForm.dataState = data.dataState;
            this.fieldDataForm.code = data.code;
            this.prpropertyFieldTypeTreeSetCurrentNode();
            this.prpropertyOtherHandle();
          }
        });
    },
    prpropertyOtherHandle() {
      if (this.fieldDataForm.enumId) {
        this.prpropertyFieldEnumTreeSetCurrentNode();
        this.enumShow = true;
      } else {
        this.enumShow = false;
      }
      if (this.fieldDataForm.fieldLength) {
        this.fieldLengthShow = true;
      } else {
        this.fieldLengthShow = false;
      }
    },
    // 菜单树选中
    prpropertyFieldTypeTreeCurrentChangeHandle(data, node) {
      this.fieldDataForm.fieldType = data.id;
      this.fieldDataForm.fieldTypeName = data.name;
    },
    // 菜单树设置当前选中节点
    prpropertyFieldTypeTreeSetCurrentNode() {
      this.$refs.prpropertyFieldTypeListTree.setCurrentKey(
        this.fieldDataForm.fieldType
      );
      this.fieldDataForm.fieldTypeName =
        (this.$refs.prpropertyFieldTypeListTree.getCurrentNode() || {})["name"];
    },

    // 枚举菜单树选中
    prpropertyFieldEnumTreeCurrentChangeHandle(data, node) {
      this.fieldDataForm.enumId = data.id;
      this.fieldDataForm.enumeName = data.name;
    },
    // 枚举 菜单树设置当前选中节点
    prpropertyFieldEnumTreeSetCurrentNode() {
      this.$refs.prpropertyFieldEnumListTree.setCurrentKey(
        this.fieldDataForm.enumId
      );
      this.fieldDataForm.enumeName =
        (this.$refs.prpropertyFieldEnumListTree.getCurrentNode() || {})["name"];
    },

    fieldDataFormSubmit() {
      this.$refs["fieldDataForm"].validate((valid) => {
        if (valid) {
          saveOrUpdateField({
            id: this.fieldDataForm.id || "",
            parentId: this.fieldDataForm.parentId || 0,
            name: this.fieldDataForm.name,
            code: this.fieldDataForm.code,
            remark: this.fieldDataForm.remark,
            tooltip: this.fieldDataForm.tooltip,
            fieldType: this.fieldDataForm.fieldType,
            orderNum: this.fieldDataForm.orderNum,
            dataState: this.fieldDataForm.dataState,
            index: this.fieldDataForm.index,
            enumId: this.fieldDataForm.enumId,
            required: this.fieldDataForm.required,
            fieldLength: this.fieldDataForm.fieldLength,
            fieldProperty: this.fieldDataForm.fieldProperty,
            defaultValue: this.fieldDataForm.defaultValue,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
